import sublime, sublime_plugin,os,sys,shutil
globals()['terrySettings'] = sublime.load_settings('DefaultDoC.sublime-settings')
#C:\Users\Administrator\AppData\Roaming\Sublime Text 3\Packages\XTool

def copyTree(orgSrcPath,desSrcPath):
	for x in os.listdir(orgSrcPath):
		tpath = os.path.join(orgSrcPath,x)
		dpath = os.path.join(desSrcPath,x)
		if os.path.isdir(tpath):
			if not os.path.exists(dpath):
				os.makedirs(dpath)
				self.searchPath(tpath,name)
		else:
			shutil.copyfile(tpath,dpath)

class xcopysyncCommand(sublime_plugin.TextCommand):
	def searchPath(self,path,name):
		print('path---',path)
		print('name---',name)
		for x in os.listdir(path):
			tpath = os.path.join(path,x)
			print ("x-->",x)
			if os.path.isdir(tpath):
				if x==name:
					self.pathMap[name] = tpath
					return
				else:
					self.searchPath(tpath,name)
	



	def run(self, edit):
		globals()['terrySettings'] = sublime.load_settings('DefaultDoC.sublime-settings')
		print (os.path.abspath('.'))
		print (os.name)
		print (sys.version)
		print (sublime.packages_path())
		print (sublime.cache_path())
		orgPath = globals()['terrySettings'].get('orgPath','')
		desPath = globals()['terrySettings'].get('desPath','')

		if orgPath=='' or desPath=='':
			sublime.message_dialog("没有配置工程路径,不能使用该功能!")
			return
		#获取工程路径
		print('orgPath',orgPath)
		folderPath = self.view.window().extract_variables().get('folder','')
		print('folderPath',folderPath)
		if folderPath == '' or not folderPath.find(orgPath)==0:
			sublime.message_dialog("工程不再此目录下,不能使用该功能!")
			return
		if self.view.is_dirty():
			self.view.run_command('save')
		desSrcPath =  os.path.join(desPath,*['simulator','win32','src'])
		desResPath =  os.path.join(desPath,*['simulator','win32','res'])
		self.pathMap = {}
		self.searchPath(orgPath,'src')
		self.searchPath(orgPath,'res')
		orgSrcPath = self.pathMap['src']
		orgResPath = self.pathMap['res']
		if not orgSrcPath or not orgResPath:
			sublime.message_dialog("工程找不到src或res目录,不能使用该功能!")
			return
		if not os.path.exists(desSrcPath):
			os.makedirs(desSrcPath)
		if not os.path.exists(desResPath):
			os.makedirs(desResPath)
		try:
			copyTree(orgSrcPath,desSrcPath)
			copyTree(orgResPath,desResPath)
			sublime.message_dialog('successful')
		except Exception as err:
			sublime.message_dialog(err)		


def getNamePath(path,name,mjname):
	tb = []
	p,f=os.path.split(path)
	while f:
		if f==name:
		   return tb
		elif f=="part":
			tb.append(mjname);
			tb.append(f);
			p,f=os.path.split(p)
		else:
			tb.append(f)
			p,f=os.path.split(p)
	return


#单拷贝,全拷贝
class filesyncCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		print("filesyncCommand")
		self.view.run_command('save')
		
		globals()['terrySettings'] = sublime.load_settings('DefaultDoC.sublime-settings')
		orgPath = globals()['terrySettings'].get('orgPath','')
		desPath = globals()['terrySettings'].get('desPath','')
		mjname = globals()['terrySettings'].get('mjname','')
		if orgPath=='' or desPath=='':
			return
		currentFilePath = self.view.file_name() 
		print ("currentFilePath-->",currentFilePath)
		path = self.view.window().extract_variables().get('folder','')
		print("orgPath",orgPath);
		if not currentFilePath or not currentFilePath.find(orgPath)==0:
			print("fanhui");
			return
		dirs,p = os.path.split(currentFilePath)
		fileName,syntax = p.split('.')
		print("syntax->"+syntax)
		if(syntax!="lua"):
			return ;
		#获取
		name_list = getNamePath(currentFilePath,'src',mjname)
		print(name_list);
		if not name_list:
			return
		desSrcPath = os.path.join(desPath,*['simulator','win32','notUpdate','src'])
		count = len(name_list)
		print('name_list',name_list)
		print('count',count)
		while count>=0:
			if(count == 0):
				shutil.copyfile(currentFilePath,desSrcPath)
				break;
			desSrcPath = os.path.join(desSrcPath,name_list[count-1])		
			if not os.path.exists(desSrcPath) and count>1:
				os.mkdir(desSrcPath)
			count = count - 1
			










       